package com.fh.fruit.service.version.impl;

import java.io.InputStream;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.multipart.MultipartFile;

import com.fh.dao.DaoSupport;
import com.fh.entity.Page;
import com.fh.fruit.entity.version.AppVersion;
import com.fh.fruit.service.version.VersionManager;
import com.fh.fruit.util.OSSUtils;
import com.fh.util.PageData;
import com.fh.util.TwoDimensionCode;

@Service("appVersionService")
public class VersionService implements VersionManager{
	@Resource(name = "daoSupport")
	private DaoSupport dao;

	private final static String endpoint="oss-cn-beijing.aliyuncs.com";   
	private final static String bucketName="fruit-picture";   
	
	@Override
	public List<PageData> appVersionlistPage(Page page)throws Exception{
		@SuppressWarnings("unchecked")
		List<PageData> list=(List<PageData>)dao.findForList("AppVsersionMapper.appVersionlistPage", page);
		return list;
	}

	@Override
	public void saveAppVersion(AppVersion av,MultipartFile pic) throws Exception{
		String content=null;
		if(av != null){
			if("iOS".equals(av.getType())){
				dao.save("AppVsersionMapper.saveAppVersion", av);
			}else{
				String au=av.getApkUrl();
				String apkUrl="http://"+bucketName+"."+endpoint+"/fruit/apk/"+av.getType()+"/"+av.getCode()+"/fruit.apk";
				String qcodeUrl="http://"+bucketName+"."+endpoint+"/fruit/apk/"+av.getType()+"/"+av.getCode()+"/fruit.png";
				av.setApkUrl(apkUrl);
				av.setQcodeUrl(qcodeUrl);
				int num=(int)dao.save("AppVsersionMapper.saveAppVersion", av);
				if(num==1 && !"iOS".equals(av.getType())){
					String upload=av.getUpload1();
					if("01".equals(upload)){
						content=apkUrl;
						String fileName="fruit/apk/"+av.getType()+"/"+av.getCode()+"/fruit.apk";
						Map<String, Object> uploadPicture=OSSUtils.pictureUpload(fileName,pic,"application/octet-stream");
						int error = (int) uploadPicture.get("error");
						if(error !=0){
							TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
						}
					}else{
						content=au;
					}
					InputStream is=TwoDimensionCode.encodeZXcode(content, "png");	
					String fileName="fruit/apk/"+av.getType()+"/"+av.getCode()+"/fruit.png";
					Map<String, Object> resMap=OSSUtils.pictureUploadI(fileName,is);
					int error = (int) resMap.get("error");
					if(error !=0){
						TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
					}
				}
			}
		}
	}

	@Override
	public void editAppVersion(AppVersion av,MultipartFile pic)throws Exception {
		String content=null;
		String au=av.getApkUrl();
		String apkUrl="http://"+bucketName+"."+endpoint+"/fruit/apk/"+av.getType()+"/"+av.getCode()+"/fruit.apk";
		String qcodeUrl="http://"+bucketName+"."+endpoint+"/fruit/apk/"+av.getType()+"/"+av.getCode()+"/fruit.png";
		av.setApkUrl(apkUrl);
		av.setQcodeUrl(qcodeUrl);
		int num=(int)dao.update("AppVsersionMapper.editAppVersion", av);
		if(num==1){
			String upload=av.getUpload1();
			if("01".equals(upload)){
				String fileName="fruit/apk/"+av.getType()+"/"+av.getCode()+"/fruit.apk";
				content=apkUrl;
				Map<String, Object> uploadPicture=OSSUtils.pictureUpload(fileName,pic,"application/octet-stream");
				int error = (int) uploadPicture.get("error");
				if(error !=0){
					TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				}
			}else{
				content=au;
			}
			InputStream is=TwoDimensionCode.encodeZXcode(content, "png");	
			
			String fileName="fruit/apk/"+av.getType()+"/"+av.getCode()+"/fruit.png";
			Map<String, Object> resMap=OSSUtils.pictureUploadI(fileName,is);
			int error = (int) resMap.get("error");
			if(error !=0){
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
		
		}
	}

	@Override
	public PageData findById(PageData pd) throws Exception{
		return (PageData)dao.findForObject("AppVsersionMapper.findById", pd);
	}

	@Override
	public void deleteAppVersion(PageData pd) throws Exception{
	     dao.delete("AppVsersionMapper.deleteAppVersion", pd);
	}

	@Override
	public int findByCode(PageData pd) throws Exception{
		// TODO Auto-generated method stub
		return (int)dao.findForObject("AppVsersionMapper.findByCode", pd);
	}



}
